<template>
<el-row :gutter="20">
  <el-col :span="7" :offset="7">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
           <el-form-item label="昵称" prop="petname">
    <el-input v-model.number="ruleForm.petname"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="checkPass">
    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="自我简介" prop="checkPass">
    <el-input
  type="textarea"
  placeholder="请输入内容"
  v-model="ruleForm.subscribes"
  maxlength="15"
  show-word-limit
>
</el-input>
  </el-form-item>
  <el-form-item label="上传头像" prop="checkPass">
    <el-upload
  class="avatar-uploader"
  action="/api/consumer-cookbook/hf_uplaod/uploadImage"
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload">
  <img v-if="ruleForm.headimage" :src=" ruleForm.headimage" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
  </el-form-item>

  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
  </el-col>
</el-row>
</template>

<script>

import axios from 'axios'
import store from '../../store/store'

  export default {
    data() {
      var checkpetname = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('昵称不能为空'));
        }
        setTimeout(() => {
          if (Number.isInteger(value)) {
            callback(new Error('不能只是数字'));
          } else {
              callback();
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {

            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          petname: '',
          headimage:'',
          subscribes:''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' },
            { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          petname: [
            { validator: checkpetname, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              var user = {
            password: this.ruleForm.pass,
          petname:this.ruleForm.petname,
          headimage:this.ruleForm.headimage,
          subscribes:this.ruleForm.subscribes
              }
            // console.log(user)
            // 提交信息
            var url = '/api/consumer-index/around_consumer/regist'
            axios.post(url,user).then(res => {
                var u = '/api/consumer-index/around_consumer/login'
                 axios.post(u,user).then(res => {
                   this.$store.dispatch("setFun", res.data);//使用了vuex进行全局状态管理
                   // localStorage.setItem("user",this.res.data)
                  // console.log(this.$store.state.user + "kkjjjj")
                    this.$router.push("/")
                 })

            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    //   上传头像的校验
     handleAvatarSuccess(res) {
         console.log(res)
         this.ruleForm.headimage = res;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
